﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace Ace
{
    public class SMSHelper
    {


        public static string UserID = Ace.Globals.Configuration["AppSettings:SMS_UserID"];
        public static string Account = Ace.Globals.Configuration["AppSettings:SMS_Account"];
        public static string Password = Ace.Globals.Configuration["AppSettings:SMS_Password"];
        public static bool SendMsg(string Phones, string Content, string SendTime)
        {
            bool ret = false;
             
            string url =Ace.Globals.Configuration["AppSettings:SMS_Host"];   //"http://61.143.63.169:8080/GateWay/Services.asmx/DirectSend";
            
            string para = "UserID=" + UserID;
            para += "&Account=" + Account;
            para += "&Password=" + Password;
            para += "&Phones=" + Phones;
            para += "&Content=" + Content;
            para += "&SendTime=" + SendTime;
            para += "&SendType=1";
            para += "&PostFixNumber=1";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(para);
            System.GC.Collect();
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myRequest.KeepAlive = false;
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;
            myRequest.Timeout = 5000;
            Stream newStream = myRequest.GetRequestStream();

            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                myResponse.Close();
                myRequest.Abort();
                ret = true;
            }
            else
            {
                myRequest.Abort();
                myResponse.Close();
            }
            return ret;
        }

        


        public static int GetLeftSmsCount()
        {
            int count = 0;
            string url = "http://61.143.63.169:8080/GateWay/Services.asmx/DirectGetStockDetails";

            string para = "?UserID=" + Ace.Globals.Configuration["AppSettings:SMS_UserID"];
            para += "&Account=" + Ace.Globals.Configuration["AppSettings:SMS_Account"];
            para += "&Password=" + Ace.Globals.Configuration["AppSettings:SMS_Password"];
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + para);

            httpWebRequest.ContentType = "Application/json";
            httpWebRequest.Method = "GET";
            httpWebRequest.Timeout = 20000;
            

            System.Net.WebRequest wReq = System.Net.WebRequest.Create(url + para);
            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(respStream);
            string srcString = reader.ReadToEnd();
            wResp.Close();
            reader.Close();

            DataSet dsData = new DataSet();
            dsData.ReadXml(new XmlTextReader(new StringReader(srcString)));
            DataTable dt = dsData.Tables["ROOT"];
            count = Convert.ToInt32(dt.Rows[0]["StockRemain"].ToString());
            return count;
        }

    }

    public enum RetCodeEnum
    {
        Sucess,
        Faild
    }

    public class SmsResult
    {
        public RetCodeEnum RetCode { get; set; }

        public string Message { get; set; }

        public int JobID { get; set; }

        public int OKPhoneCounts { get; set; }

        public int StockReduced { get; set; }

        public string ErrPhones { get; set; }
    }

    public class SmsQueryResult
    {
        public RetCodeEnum RetCode { get; set; }

        public string Message { get; set; }

        public int StockRemain { get; set; }

        public int Points { get; set; }

        public int SendTotal { get; set; }

        public int CurDaySend { get; set; }

    }

}
